package fun.yizhierha.operation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import fun.yizhierha.common.base.BaseErrDto;
import fun.yizhierha.common.exception.BadRequestException;
import fun.yizhierha.common.exception.BizCodeEnum;
import fun.yizhierha.common.utils.PageUtils;
import fun.yizhierha.common.utils.Query;
import fun.yizhierha.common.utils.SecurityUtils;
import fun.yizhierha.common.utils.ValidList;
import fun.yizhierha.common.utils.file.ExcelUtils;
import fun.yizhierha.operation.domain.OraDatabase;
import fun.yizhierha.operation.domain.vo.CreateOraDatabaseVo;
import fun.yizhierha.operation.domain.vo.UpdateOraDatabaseVo;
import fun.yizhierha.operation.domain.vo.RetrieveOraDatabaseVo;
import fun.yizhierha.operation.mapper.OraDatabaseMapper;
import fun.yizhierha.operation.service.mapstruct.OraDatabaseMapstruct;
import fun.yizhierha.operation.service.OraDatabaseService;
import fun.yizhierha.operation.util.SqlUtils;
import net.sf.jsqlparser.schema.Database;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

/** generated by EH-Admin
* @author xaopohi
* @date Wed Dec 21 16:08:00 CST 2022
**/
@Service
@RequiredArgsConstructor
public class OraDatabaseServiceImpl extends ServiceImpl<OraDatabaseMapper, OraDatabase> implements OraDatabaseService{

    private final OraDatabaseMapstruct oraDatabaseMapstruct;

    @Override
    public PageUtils<OraDatabase> list(RetrieveOraDatabaseVo retrieveOraDatabaseVo, Query.PageVo pageVo) {
        QueryWrapper<OraDatabase> wrapper = new QueryWrapper<>();
        String name = retrieveOraDatabaseVo.getName();
        String userName = retrieveOraDatabaseVo.getUserName();
        String typeDatabese = retrieveOraDatabaseVo.getTypeDatabese();

        if (name != null){
            wrapper.like(OraDatabase.COL_NAME,name);
        }
        if (userName != null){
            wrapper.like(OraDatabase.COL_USER_NAME,userName);
        }
        if (typeDatabese != null){
            wrapper.eq(OraDatabase.COL_TYPE_DATABESE,typeDatabese);
        }


        IPage<OraDatabase> iPage = baseMapper.selectPage(new Query<OraDatabase>().getPage(pageVo), wrapper);
        return new PageUtils<>(iPage);
    }

    @Override
    public synchronized void save(CreateOraDatabaseVo createOraDatabaseVo) {
        // 1.字段为UNI，需要不重复
        UserDetails currentUser = SecurityUtils.getCurrentUser();
        // 2.映射数据
        OraDatabase oraDatabase = oraDatabaseMapstruct.toOraDatabase(createOraDatabaseVo);
        oraDatabase.setCreateTime(new Timestamp(new Date().getTime()));
        oraDatabase.setCreateBy(currentUser.getUsername());
        // 3.保存    
        this.save(oraDatabase);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public synchronized void edit(ValidList<UpdateOraDatabaseVo> updateOraDatabaseVoList, List<BaseErrDto> errDtoList) {
        List<OraDatabase> toUpdateOraDatabaseList = new ArrayList<>();

        for (UpdateOraDatabaseVo updateOraDatabaseVo : updateOraDatabaseVoList) {
            Long id = updateOraDatabaseVo.getId();
            // 1.字段为UNI，需要不重复
            UserDetails currentUser = SecurityUtils.getCurrentUser();
            updateOraDatabaseVo.setUpdateBy(currentUser.getUsername());
            OraDatabase oraDatabase = oraDatabaseMapstruct.toOraDatabase(updateOraDatabaseVo);
            oraDatabase.setUpdateTime(new Timestamp(new Date().getTime()));

            toUpdateOraDatabaseList.add(oraDatabase);
        }

        // 2.更新
        this.updateBatchById(toUpdateOraDatabaseList);
    }

    @Override
    public void remove(Set<Long> ids) {
        this.removeByIds(ids);
    }

    @Override
    public void download(HttpServletResponse response) {
        ExcelUtils.export(response,"部署管理信息表",this.list(), OraDatabase.class);
    }

    @Override
    public Boolean testConnection(Long dbId) {
        try {
            if (dbId == null) return false;
            OraDatabase resources = this.getById(dbId);
            if (resources == null) return false;
            return SqlUtils.testConnection(resources.getJdbcUrl(), resources.getUserName(), resources.getPwd());
        } catch (Exception e) {
            log.error(e.getMessage());
            return false;
        }
    }

}